﻿Imports Business
Imports Entities
Imports DataAccess
Public Class IngresoGuia
    Private Sub IngresoGuia_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
#Region "Metodos Privados"
    Private Sub Titulo()
        With gGrilla
            Me.Cursor = Cursors.WaitCursor
            Dim lista = GuiaBL.GetDetalleGuiaP(CLng(tNumeroFactura.Text))
            Me.Cursor = Cursors.Default
            If IsNothing(lista) Then
                Util.Mensaje("No Existen Pendientes para la Factura Ingresada")
                Exit Sub
            End If
            .DataSource = lista
            .Font = New Drawing.Font("Tahoma", Util.GridFontIngresos, FontStyle.Regular, GraphicsUnit.Point)
            .RowHeadersVisible = False

            .Columns("ITEMid").Visible = False
            For Each col As DataGridViewColumn In gGrilla.Columns
                col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
            Next
            '.Columns("DESCRIPCION").Width = 180
            '.Columns("CantidadTotal").Width = 100
            '.Columns("CantidadIngresada").Width = 100
            '.Columns("CantidadPendiente").Width = 100
            '.Columns("CantidadGuia").Width = 110
            '.Columns("PrecioUnitario").Width = 120
            '.Columns("Total").Width = 100

            .Columns("CantidadGuia").DefaultCellStyle.BackColor = Color.Aquamarine

            .Columns("DESCRIPCION").ReadOnly = True
            .Columns("CantidadIngresada").ReadOnly = True
            .Columns("CantidadPendiente").ReadOnly = True
            .Columns("PrecioUnitario").ReadOnly = True
            .Columns("Total").ReadOnly = True

            .Columns("DESCRIPCION").HeaderText = "Descripcion Item"
            .Columns("CantidadTotal").HeaderText = "Cantidad Total"
            .Columns("CantidadIngresada").HeaderText = "Cantidad Ingresada"
            .Columns("CantidadPendiente").HeaderText = "Cantidad Pendiente"
            .Columns("CantidadGuia").HeaderText = "Cantidad Guia Despacho"
            .Columns("PrecioUnitario").HeaderText = "Precio Unitario"
            .Columns("Total").HeaderText = "Total"

            For Each col As DataGridViewColumn In gGrilla.Columns
                col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter
            Next
            .Columns("DESCRIPCION").DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomLeft
            .Columns("CantidadTotal").DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
            .Columns("CantidadIngresada").DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
            .Columns("CantidadPendiente").DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
            .Columns("CantidadGuia").DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
            .Columns("PrecioUnitario").DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
            .Columns("Total").DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter

            .Columns("PrecioUnitario").DefaultCellStyle.Format = "$ ##,##"
            .Columns("Total").DefaultCellStyle.Format = "$ ##,##"
        End With
    End Sub

    Private Sub CalcularTotales()
        Dim TotalKilos, total As Integer

        For Each Fila As DataGridViewRow In gGrilla.Rows
            TotalKilos += Fila.Cells("CantidadGuia").Value * ItemBL.GetKilos(Fila.Cells("ItemId").Value)
            total += Fila.Cells("PrecioUnitario").Value * Fila.Cells("CantidadGuia").Value
            Fila.Cells("Total").Value = Fila.Cells("CantidadGuia").Value * Fila.Cells("PrecioUnitario").Value
            Fila.Cells("CantidadPendiente").Value = Fila.Cells("CantidadTotal").Value - (Fila.Cells("CantidadGuia").Value + Fila.Cells("CantidadIngresada").Value)
        Next

        tTotal.Text = FormatCurrency(total)
        tKilos.Text = FormatNumber(TotalKilos)
        'tNeto.Text = FormatCurrency(CLng(tSubtotal.Text) - (CLng(tDesctoExclu.Text) + CLng(tDesctoVolumen.Text)))
        'tIva.Text = (FormatCurrency((CLng(tNeto.Text) * Util.CurrentParam.VALORIVA)))
        'tTotal.Text = FormatCurrency(CLng(tIva.Text) + CLng(tIvaSM.Text) + CLng(tNeto.Text))

    End Sub
#End Region

    Private Sub tNumeroFactura_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles tNumeroFactura.Leave
        If tNumeroFactura.Text <> "" And Val(tNumeroFactura.Text) > 0 Then
            Dim factura = FacturaBL.SearchById(CLng(tNumeroFactura.Text))
            If IsNothing(factura) Then
                Util.MensajeError("La Factura No Existe")
                gGrilla.DataSource = Nothing
                tNumeroFactura.Focus()
                Exit Sub
            End If

            Titulo()
        End If
    End Sub

    Private Sub gGrilla_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gGrilla.CellEnter
        If e.ColumnIndex = gGrilla.Columns.IndexOf(gGrilla.Columns("CantidadGuia")) Then
            Using Form As New IngresoDato("Ingreso Cantidad de Compra", "Ingrese Cantidad")
                Form.ShowDialog()
                If Form.DialogResult = DialogResult.OK Then
                    gGrilla.Item(e.ColumnIndex, e.RowIndex).Value = Form.GetValue
                    CalcularTotales()
                End If
            End Using
        End If
    End Sub

    Private Sub bCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bCancelar.Click
        Dispose()
    End Sub

    Private Sub bIngresarGuia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bIngresarGuia.Click
        If CLng(tNumeroFactura.Text) = 0 Then
            Util.Mensaje("Debe Ingresar una factura")
            tNumeroFactura.Focus()
            Exit Sub
        End If
        If CLng(Val(tNumeroGuia.Text)) = 0 Then
            Util.Mensaje("Debe Ingresar el Número de la Guia")
            tNumeroGuia.Focus()
            Exit Sub
        End If
        If CLng(tKilos.Text) = 0 Then
            Util.Mensaje("Debe Ingresar por lo Menos un Item de la Guía")
            gGrilla.Focus()
            Exit Sub
        End If

        Dim guia As New GUIAS
        With guia
            .GUIA = CLng(tNumeroGuia.Text)
            .FACTURA = CLng(tNumeroFactura.Text)
            .PRECIOTOTAL = CLng(tTotal.Text)
            .TOTALKILOS = CLng(tKilos.Text)
            .DESCRIPCION = ""
        End With
        Dim detalle As New List(Of DETALLEGUIA)
        For Each fila As DataGridViewRow In gGrilla.Rows
            If fila.Cells("CantidadGuia").Value > 0 Then
                Dim det As New DETALLEGUIA
                With det
                    .ITEM = CLng(fila.Cells("ItemId").Value)
                    .CANTIDAD = CLng(fila.Cells("CantidadGuia").Value)
                End With
                detalle.Add(det)
            End If
        Next
        guia = GuiaBL.Save(guia, detalle)
      
        If Not IsNothing(guia) Then
            Util.Mensaje("Guia Ingresada Correctamente")
        Else
            Util.MensajeError("Error al Ingresar Guia")
        End If
        Me.Dispose()
    End Sub

    Private Sub tNumeroGuia_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles tNumeroGuia.Leave
        If tNumeroGuia.TextLength > 0 Then
            Dim guia As New GUIAS
            guia = GuiaBL.SearchById(CLng(tNumeroGuia.Text))
            If Not IsNothing(guia) Then
                Util.Mensaje("Esta Guia ya se ingresó al Registro")
                tNumeroGuia.Focus()
            End If
        End If
    End Sub

    Private Sub tNumeroFactura_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tNumeroFactura.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            Util.Siguiente()
        End If
    End Sub

    Private Sub tNumeroGuia_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tNumeroGuia.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            Util.Siguiente()
        End If
    End Sub

    Private Sub tFechaGuia_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tFechaGuia.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            Util.Siguiente()
        End If
    End Sub

    Private Sub tKilos_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tKilos.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            Util.Siguiente()
        End If
    End Sub

    Private Sub tTotal_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tTotal.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            Util.Siguiente()
        End If
    End Sub
End Class